package com.zkca.xf.ui.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zkca.xf.ConstantsXF;
import com.zkca.xf.Constants_SP;
import com.zkca.xf.MyApplication;
import com.zkca.xf.R;
import com.zkca.xf.base.BaseTitleActivity;
import com.zkca.xf.model.javabean.ServerSelectBean;
import com.zkca.xf.presenter.SelectServerPresenter;
import com.zkca.xf.ui.adapter.LBaseAdapter;
import com.zkca.xf.ui.adapter.LViewHolder;
import com.zkca.xf.utils.LogUtil;
import com.zkca.xf.utils.SPUtil;
import com.zkca.xf.utils.XmlHelper;
import com.zkca.xf.view.SelectServerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.zkca.xf.R.mipmap.fuwu1;

/**
 * Created by pc on 2018/8/2.
 * 选择服务器
 */

public class Activty_Select_Server extends BaseTitleActivity implements SelectServerView {
    @BindView(R.id.lv_area)
    ListView lv_area;

    private SelectServerPresenter ssPresenter;
    List<ServerSelectBean> serviceList;
    LBaseAdapter<ServerSelectBean> adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activty_select_server);
        ButterKnife.bind(this);
        ssPresenter=new SelectServerPresenter(this, this);
        initview();
        initdata();
    }


    private void initview() {
        setTitleText("地区选择");
    }

    private void initdata() {
        ssPresenter.SelectServer();




    }
    @Override
    public void setTitleText(String c) {
        super.setTitleText(c);
    }

    @Override
    public void showProgressDialog() {

    }

    @Override
    public void hideProgressDialog() {

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void getSelectServer(String serverSelectBean) {
        serviceList = XmlHelper.getInstance().getList(ServerSelectBean.class, serverSelectBean, "service_name");
        adapter = new LBaseAdapter<ServerSelectBean>(R.layout.item_area, serviceList, mContext) {

            @Override
            public void setContent(LViewHolder vh, ServerSelectBean data, int position) {
                RelativeLayout rl_server = (RelativeLayout) vh.getViews(R.id.rl_server);
                TextView lv_area = (TextView) vh.getViews(R.id.tv_city);
                lv_area.setText(data.getService_name());
                if(position%2==0) {
                    Drawable drawable = getResources().getDrawable(R.mipmap.fuwu1);
                    rl_server.setBackgroundDrawable(drawable);
                }else{
                    Drawable drawable = getResources().getDrawable(R.mipmap.fuwu2);
                    rl_server.setBackgroundDrawable(drawable);
                }
            }
        };
        lv_area.setAdapter(adapter);

        lv_area.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                SPUtil.putString(Activty_Select_Server.this,Constants_SP.SERVER_ADDRESS,serviceList.get(position).getService_url());
                SPUtil.putString(Activty_Select_Server.this,Constants_SP.CITY,serviceList.get(position).getService_name());
                gotoActivity(Activity_Login_Code.class,true);
                 String server_address = SPUtil.getString(MyApplication.getInstance(), Constants_SP.SERVER_ADDRESS, "");

                ConstantsXF. URL_SERVER_CE=server_address;

//                String server_address = sp.getString("SERVER_ADDRESS", "");
//                String city = sp.getString("CITY", "");
//                Log.e("lcq","server_address="+server_address+",city="+city);
                Activty_Select_Server.this.finish();
            }
        });
    }
}
